package oppsa.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import oppsa.controller.Translate;
import oppsa.model.ActionDB;
import oppsa.model.ProjectDB;
import oppsa.model.SSComponentDB;
import oppsa.model.SubsystemDB;
import oppsa.util.Session;

import org.jdesktop.swingx.JXFrame;

public class AddSubSystBox extends JXFrame {
	private static final long serialVersionUID = 1L;
	private JTextField textFieldSubSystem;
	private JPanel panel;
	private JLabel lblAddSubSystem;
	private JLabel lblSubSystem;
	private JButton btnSubmit;
	private SubsystemDB listSubSystem;
	private ActionDB action;
	private SSComponentDB listSSComponent;
	private JButton source;
	private JButton btnCancel;
	private ActionDetailsView adv;
	private HomeView hv;
	private AddSSComponentBox asc;

	/**
	 * @wbp.parser.constructor
	 */
	public AddSubSystBox(final HomeView hv, final AddSSComponentBox asc, final boolean rename, String name) {
		addWindowListener(closeWindow);
		setIconImage(Toolkit.getDefaultToolkit().getImage(AddSubSystBox.class.getResource("/LOGO_FONCE.png")));
		this.hv = hv;
		this.asc = asc;
		this.listSubSystem = new SubsystemDB();
		this.action = new ActionDB();
		final Translate t = new Translate();

		if(asc!=null) {
			asc.addWindowFocusListener(clickhomeview);
		}else{
			hv.addWindowFocusListener(clickhomeview);
		}
		
		setStartPosition(StartPosition.CenterInScreen);
		getRootPaneExt().getContentPane().setBackground(Color.RED);
		setMinimumSize(new Dimension(400, 200));
		setPreferredSize(new Dimension(400, 200));
		setResizable(false);
		if (!rename) {
			setTitle(t.getLocalizedText("tlAddSubSystem"));
		} else {
			setTitle(t.getLocalizedText("tlRenameSubSystem"));
		}
		panel = new JPanel();
		panel.setBackground(Color.WHITE);
		getRootPaneExt().getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new MigLayout("", "[][grow]", "[][grow][][][][grow]"));
		if (!rename) {
			lblAddSubSystem = new JLabel(t.getLocalizedText("lblAddSubSystem"));
		} else {
			lblAddSubSystem = new JLabel(t.getLocalizedText("lblRenameSubSystem"));
		}
		lblAddSubSystem.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel.add(lblAddSubSystem, "cell 0 0");
		
		if (!rename) {
			lblSubSystem = new JLabel(t.getLocalizedText("lblSubSystem"));
		} else {
			lblSubSystem = new JLabel(t.getLocalizedText("lblRenameSubSystem"));
		}
		lblSubSystem.setFont(new Font("Tahoma", Font.BOLD, 11));
		panel.add(lblSubSystem, "cell 0 2,alignx left");

		textFieldSubSystem = new JTextField(name);
		panel.add(textFieldSubSystem, "cell 1 2,alignx left");
		textFieldSubSystem.setColumns(20);

		btnSubmit = new JButton("");
		btnSubmit.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Validate"))));
		btnSubmit.setBorder(null);
		btnSubmit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				if (textFieldSubSystem.getText().equals("")) {
					textFieldSubSystem.setBackground(Color.RED);
					JOptionPane.showMessageDialog(AddSubSystBox.this, "Aucune valeur saisie");
				} else if (!rename){
					listSubSystem = new SubsystemDB();
					if (!listSubSystem.existe(textFieldSubSystem.getText()))
					{
						ProjectDB p = new ProjectDB();
						p.select(Session.userSequence);
						listSubSystem.insert(textFieldSubSystem.getText(),p.getCurrentProject().getSequence());
						listSSComponent = new SSComponentDB();
						listSubSystem.select();
						listSSComponent.insert(listSubSystem.getSequenceFromName(textFieldSubSystem.getText())," -");
						setVisible(false);
						if(asc!=null) {
							
							asc.setVisible(true);
							asc.setEnabled(true);
							asc.toFront();
						}else{
						
							hv.setVisible(true);
							hv.setEnabled(true);
							hv.toFront();
						}
						if (AddSubSystBox.this.asc !=null){
							AddSubSystBox.this.asc.updateSubSystem(textFieldSubSystem.getText());
						}
						AddSubSystBox.this.hv.refreshTree2();
					} else {
						JOptionPane.showMessageDialog(AddSubSystBox.this,t.getLocalizedText("lblDuplicateTree"),t.getLocalizedText("lblSubSystemWindow"),JOptionPane.INFORMATION_MESSAGE);
					}
				}else{
					
					listSubSystem.selectBySubSystem(AddSubSystBox.this.hv.getSelectedSSComp());
					int seq = listSubSystem.getFullList().get(0).getSequence();
					ProjectDB p = new ProjectDB();
					p.select(Session.userSequence);
					listSubSystem.renameSubSystBySeq(seq, textFieldSubSystem.getText());
					action.updateActionIDbyMeetingType(seq, false);
					AddSubSystBox.this.hv.refreshTree2();
					if(asc!=null) {
						setVisible(false);
						asc.setVisible(true);
						asc.setEnabled(true);
						asc.toFront();
					}else{
						setVisible(false);
						hv.setVisible(true);
						hv.setEnabled(true);
						hv.toFront();
					}
					
				}
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("SubmitS"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
			}
		});
		

		btnCancel = new JButton("");
		btnCancel.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
		btnCancel.setBorder(null);
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				setVisible(false);
				if(asc!=null) {
					asc.setVisible(true);
					asc.setEnabled(true);
					asc.toFront();
				}else{
					hv.setVisible(true);
					hv.setEnabled(true);
					hv.toFront();
				}
				
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("CancelS"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
			}
		});
		panel.add(btnCancel, "cell 1 4");
		panel.add(btnSubmit, "cell 1 4");
	}

	
	WindowAdapter closeWindow = new WindowAdapter() {
		@Override
		public void windowClosing(WindowEvent arg0) {
			
			setVisible(false);
			if(asc!=null) {
				setVisible(false);
				asc.setVisible(true);
				asc.setEnabled(true);
				asc.toFront();
			}else{
				setVisible(false);
				hv.setVisible(true);
				hv.setEnabled(true);
				hv.toFront();
			}
		}
	};
	
	WindowFocusListener  clickhomeview = new  WindowFocusListener () {

		@Override
		public void windowGainedFocus(WindowEvent arg0) {
			// TODO Auto-generated method stub
			toFront();
		}

		@Override
		public void windowLostFocus(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}
	};
}
